<template>
  <div>
    <div>
      <ele-page
        v-loading="loading"
        :formConfig="searchConfig"
        ref="eleTable"
        :operateList="operateList"
        :column="column"
        :getList="getList"
        uniqueId="mesProdBoxcodeTable"
        :initForm="otherParams"
        :beforeAjax="beforeAjax"
      ></ele-page>
    </div>
    <ele-form-drawer
      :isLoading="isLoading"
      v-model="formDataDialog"
      v-bind="formDescDialog"
      :request-fn="handleSubmit"
      :visible.sync="visible"
      :title="title"
    ></ele-form-drawer>
  </div>
</template>

<script>
import { operateList, searchConfig, formDescDialog, rowConfig } from "./config";
import { getList, getYesOrNot } from "@/api/print/mesCapacityCodeInfo";
import {
  templateType, //获取生码分类
} from "@/api/print/mesCapacityCode";
export default {
  name: "mesProdBoxcode",
  data() {
    return {
      title: null,
      visible: false,
      isLoading: false,
      loading: false,
      formDataDialog: {},
      formDescDialog, //新增修改框
      searchConfig,
      operateList,
      getList,
      otherParams: this.$route.query,
      // 列表页字段的绑定配置
      column: [
        {
          type: "selection",
          fixed: "left",
        },
        {
          type: "index",
          label: $i18n.t("common.order"),
          width: 60,
          fixed: "left",
        },
        {
          prop: "barcode",
          label: $i18n.t("capacityCodeInfo.uniqueCode"),
          minWidth: 300,
          showOverflowTooltip: true,
          fixed: "left",
        },
        {
          prop: "planCode",
          label: $i18n.t("capacityCodeInfo.planCode"),
          minWidth: 150,
          showOverflowTooltip: true,
        },
        {
          prop: "orderCode",
          label: $i18n.t("capacityCodeInfo.orderCode"),
          minWidth: 180,
          showOverflowTooltip: true,
        },
        {
          prop: "prodCode",
          label: $i18n.t("capacityCodeInfo.modelCode"),
          minWidth: 150,
          showOverflowTooltip: true,
        },
        {
          prop: "prodName",
          label: $i18n.t("capacityCodeInfo.modelName"),
          minWidth: 200,
          showOverflowTooltip: true,
        },
        {
          prop: "count",
          label: $i18n.t("capacityCodeInfo.volume"),
          minWidth: 100,
          showOverflowTooltip: true,
        },
        {
          prop: "unit",
          label: $i18n.t("capacityCodeInfo.unit"),
          minWidth: 100,
          showOverflowTooltip: true,
        },

        // {
        //   prop: "isInvalid",
        //   label: "是否作废",
        //   minWidth: 100,
        //   showOverflowTooltip: true,
        //   render: (h, { row }) => {
        //     return row.isInvalid === "0" ? <p>否</p> : <p>是</p>;
        //   },
        // },
        {
          prop: "capacityType",
          label: $i18n.t("capacityCodeInfo.printSort"),
          minWidth: 100,
          showOverflowTooltip: true,
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            const params = {};
            const list = await templateType(params);
            return list.data.result;
          },
        },
        {
          prop: "seriesCode",
          label: $i18n.t("capacityCodeInfo.seriesCode"),
          minWidth: 150,
          showOverflowTooltip: true,
        },
        {
          prop: "seriesName",
          label: $i18n.t("capacityCodeInfo.seriesName"),
          minWidth: 200,
          showOverflowTooltip: true,
        },
        // {
        //   prop: "count",
        //   label: $i18n.t("mesProdUniqueCode.volume")
        // },
        {
          prop: "planBeginDate",
          label: $i18n.t("capacityCodeInfo.planBeginDate"),
          minWidth: 150,
          showOverflowTooltip: true,
        },
        {
          prop: "planEndDate",
          label: $i18n.t("capacityCodeInfo.planEndDate"),
          minWidth: 150,
          showOverflowTooltip: true,
        },
        {
          prop: "barcodePrintTemp",
          label: $i18n.t("capacityCodeInfo.barcodePrintTemp"),
          minWidth: 170,
          showOverflowTooltip: true,
        },
        {
          prop: "lineCode",
          label: $i18n.t("common.lineCode"),
          minWidth: 150,
          showOverflowTooltip: true,
        },
        {
          prop: "lineName",
          label: $i18n.t("common.lineName"),
          minWidth: 150,
          showOverflowTooltip: true,
        },

        {
          prop: "isPrint",
          label: $i18n.t("capacityCodeInfo.isPrint"),
          minWidth: 100,
          showOverflowTooltip: true,
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            const list = await getYesOrNot();
            return list.data.result;
          },
        },
        // {
        //   prop: "isSubmitted",
        //   label: $i18n.t("capacityCodeInfo.isSubmitted"),
        //   minWidth: 100,
        //   showOverflowTooltip: true,
        //   optionProp: {
        //     value: "value",
        //     label: "description",
        //   },
        //   option: async (data) => {
        //     const list = await getYesOrNot();
        //     return list.data.result;
        //   },
        // },
        {
          prop: "createByName",
          label: $i18n.t("common.createByName"),
          minWidth: 140,
          showOverflowTooltip: true,
        },
        {
          prop: "createTime",
          label: $i18n.t("common.createTime"),
          minWidth: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "updateByName",
          label: $i18n.t("common.updateByName"),
          minWidth: 140,
          showOverflowTooltip: true,
        },
        {
          prop: "updateTime",
          label: $i18n.t("common.updateTime"),
          minWidth: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "operate",
          label: $i18n.t("common.operation"),
          width: 120,
          fixed: "right",
          render: (h, scope) => {
            return (
              <ele-row-operation
                scope={scope}
                rowConfig={rowConfig}
                onRowOperate={this.rowOperate}
              />
            );
          },
        },
      ],
    };
  },
  computed: {},
  methods: {
    beforeAjax(data) {
      return { ...this.otherParams, ...data };
    },
    //表格行操作事件
    rowOperate({ handleName, name, row }) {
      this.title = name;
      const fnc = {
        show: () => {
          // 修改详情，并打开界面
          this.editInfo(row, true);
        },
        edit: () => {
          // 修改详情，并打开界面
          this.editInfo(row, false);
        },
      };
      fnc[handleName] && fnc[handleName]();
    },
    //弹框提交事件
    handleSubmit(data) {
      if (this.isLoading) return; // 判断状态
      this.isLoading = true;
      this.formDataDialog.id ? this.rowUpdate(data) : this.rowCreate(data);
    },
    //新增
    rowCreate(data) {
      rowCreate(data)
        .then((res) => {
          const message = res.data.message;
          this.$message.success(message || $i18n.t("common.addSuccess"));
          this.visible = false;
          this.refresh();
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
    // 修改详情
    editInfo(row, flag) {
      console.log(row);
      this.changeSub(flag);
      this.formDataDialog = row;
      this.visible = true;
    },
    //修改
    rowUpdate(data) {
      rowUpdate(data)
        .then((res) => {
          const message = res.data.message;
          this.$message.success(message || $i18n.t("common.editSuccess"));
          this.visible = false;
          this.refresh();
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
    //删除
    rowDel(data) {
      this.loading = true;
      rowDel(data)
        .then((res) => {
          const message = res.data.message;
          this.$message.success(message || $i18n.t("common.deleteSuccess"));
          this.refresh();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //刷新表格数据
    refresh(isReset = true) {
      this.$refs.eleTable.refresh(isReset);
    },
    changeSub(flag) {
      if (flag) {
        formDescDialog.disabled = true;
        formDescDialog.isShowSubmitBtn = false;
      } else {
        formDescDialog.disabled = false;
        formDescDialog.isShowSubmitBtn = true;
      }
    },
  },
};
</script>
